package com.ts.ischool.ui.ctrl;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.ts.ischool.pojo.UserBean;
import com.ts.ischool.service.UserService;

public class AuthorizedController extends SelectorComposer<Component> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4155159995818610938L;
	
	private UserService userService;
	
	@Wire
	private Textbox username;
	
	@Wire
	private Textbox password;
	
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		comp.setAttribute("controller", this);
		
	}

	@Listen("onClick = #loginwithgoogle")
	public void loginWithGoogle() {
		String loginName = username.getValue();
		String passWord = password.getValue();
		UserBean user = userService.checkUser(loginName.trim(), passWord.trim());
		if (loginName.contains("gmail")) {
			if (null == user) {
				Messagebox.show("Inviable User Account!");
			} else {
				Executions.sendRedirect("https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Fmyschool.jelastic.servint.net%2Fischool%2Fredirect.zul&response_type=token&client_id=1083338644751.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar+https%3A%2F%2Fmail.google.com%2Fmail%2Ffeed%2Fatom&approval_prompt=force&state="+loginName);
			}
		} else {
			Messagebox.show("Inviable User Account!");
		}
		
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
